loadstring(exports.dgs:dgsImportFunction())()

local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
cmtaSALON = false

font = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", (11.5)*px )


addEvent("cmta_salon:showGUI", true)
addEventHandler("cmta_salon:showGUI", resourceRoot, function()
	window = dgsCreateWindow(1100*px,300*py,250*px,450*py,"Salon",false)
	dgsWindowSetCloseButtonEnabled(window, false)
	dgsWindowSetMovable (window, false )
	dgsWindowSetSizable (window, false )
	scrollpanel = dgsCreateComboBox(20*px,30*py,210*px,20*py,"Wybierz samochód",false,window)
	button = dgsCreateButton( 25*px, 260*py, 200*px, 40*py, "Kup", false,window,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
	button2 = dgsCreateButton( 25*px, 310*py, 200*px, 40*py, "Jazda próbna", false,window,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
	button3 = dgsCreateButton( 25*px, 360*py, 200*px, 40*py, "Zamknij", false,window,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
	text = dgsCreateLabel(0*px,70*py,250*px,150*py,"",false,window)
	dgsSetFont( text, font )
	dgsSetProperty(text,"colorcoded",true)
	dgsSetProperty(text,"alignment",{"center","center"})
	cmtaSALON = true
	triggerServerEvent("cmta_salon:sgetvehs", resourceRoot)
	showCursor(true)
	addEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
	addEventHandler("onDgsComboBoxSelect",getRootElement(),onselected)
	setCameraMatrix(-1953.79968, 265.65643, 35.46875+2,-1947.64600, 259.17596, 35.46875)

end)

addEvent("cmta_salon:hideGUI", true)
addEventHandler("cmta_salon:hideGUI", resourceRoot, function()
	if cmtaSALON then
	removeEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
	removeEventHandler ("onDgsComboBoxSelect",getRootElement(),onselected) 
	destroyElement(window)
	if isElement(window2) then destroyElement(window2) end
	showCursor(false)
	cmtaSALON = false
	if isElement(getElementData(localPlayer,"salon:vehicle")) then
		destroyElement(getElementData(localPlayer,"salon:vehicle"))
		setElementData(localPlayer,"salon:vehicle",nil)
	end
	setCameraTarget(localPlayer,localPlayer)
	setElementFrozen(localPlayer,false)	
	end
end)

function onclick(btn,state)
	if btn=="left" and state=="down" and cmtaSALON then
		if source == button then
			local item = dgsComboBoxGetSelectedItem(scrollpanel)
			if item ~= -1 then
				local color = dgsComboBoxGetItemColor( scrollpanel, item )
				if color == -65536 then outputChatBox("#ff2828Zabrakło pojazdów tego modelu, poczekaj na dostawę!",255,255,255,true) return end
				dgsSetEnabled ( window, false )
				window2 = dgsCreateWindow(600*px,582*py,466*px,168*py,"Wybierz specyfikację",false)
				dgsWindowSetCloseButtonEnabled(window2, false)
				dgsWindowSetMovable (window2, false )
				dgsWindowSetSizable (window2, false )
				button4 = dgsCreateButton( 80*px, 90*py, 166*px, 40*py, "Potwierdź", false,window2,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
				colorpicker = dgsCreateColorPicker("HSDisk",320*px,10*py,120*px,120*py,false,window2)
				rbutton1 = dgsCreateRadioButton(20*px,20*py,100*px,30*py," Benzyna 1.0cm³",false,window2)
				rbutton2 = dgsCreateRadioButton(160*px,20*py,100*px,30*py," Diesel 1.1cm³",false,window2)
				--triggerServerEvent("cmta_salon:buy", localPlayer,dgsComboBoxGetItemText(scrollpanel, item))
			end
		end
		if source == button2 then
			local item = dgsComboBoxGetSelectedItem(scrollpanel)
			if item ~= -1 then
				triggerServerEvent("cmta_salon:testdrive", localPlayer,dgsComboBoxGetItemText(scrollpanel, item))
			end
		end
		if source == button3 then
		    destroyElement(window)
			showCursor(false)
			cmtaSALON = false
			if isElement(getElementData(localPlayer,"salon:vehicle")) then
				destroyElement(getElementData(localPlayer,"salon:vehicle"))
				setElementData(localPlayer,"salon:vehicle",nil)
			end
			removeEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
			removeEventHandler  ("onDgsComboBoxSelect",getRootElement(),onselected)
			setCameraTarget(localPlayer,localPlayer)
			setElementFrozen(localPlayer,false)
		end
		if source == button4 then
			local item = dgsComboBoxGetSelectedItem(scrollpanel)
			if item ~= -1 then
				if dgsRadioButtonGetSelected( rbutton1 ) then
				local r,g,b,a = dgsColorPickerGetColor(colorpicker,"RGB")
				triggerServerEvent("cmta_salon:buy", localPlayer,dgsComboBoxGetItemText(scrollpanel, item),1,r,g,b)
			else
				local r,g,b,a = dgsColorPickerGetColor(colorpicker,"RGB")
				triggerServerEvent("cmta_salon:buy", localPlayer,dgsComboBoxGetItemText(scrollpanel, item),2,r,g,b)
			end
		    end
		end
		if source == colorpicker then
			local r,g,b,a = dgsColorPickerGetColor(colorpicker,"RGB")
			setVehicleColor(getElementData(localPlayer,"salon:vehicle"),r,g,b)
		end
	end
end

addEvent("cmta_salon:getvehs", true)
addEventHandler("cmta_salon:getvehs", resourceRoot, function(result)
	for i,v in pairs(result) do
	local item = dgsComboBoxAddItem(scrollpanel, v["name"])
	if v["pcs"] == 0 then
	dgsComboBoxSetItemColor ( scrollpanel , item, tocolor( 255,0,0,255))
	end
	end
end)

addEvent("cmta_salon:info", true)
addEventHandler("cmta_salon:info", resourceRoot, function(result)
	if isElement(getElementData(localPlayer,"salon:vehicle")) then
		destroyElement(getElementData(localPlayer,"salon:vehicle"))
		setElementData(localPlayer,"salon:vehicle",nil)
	end
	vehicle_salon = createVehicle(tonumber(result[1]["id"]),-1947.64600, 259.17596, 35.46875,0,0,0)
	setElementData(localPlayer,"salon:vehicle",vehicle_salon)
	if result[1]["shader"] == "Tak" then
		kolsha = "#00ff00Tak"
	else
		kolsha = "#ff0000Nie"
	end
	if result[1]["pcs"] >= 0 and result[1]["pcs"] <= 3 then
		pcscolor = "#ff0000"
	elseif result[1]["pcs"] > 3 and result[1]["pcs"] < 10 then
		pcscolor = "#ccc000"
	elseif result[1]["pcs"] >= 10 then
		pcscolor = "#00ff00"
	end
	dgsSetText(text,"Silnik: #ff00001.0 cm³\n#ffffffV-max: #ff0000"..result[1]["vmax"].." km/h#ffffff\nMożliwość malowania: "..kolsha.."\n#ffffffPojazd został zakupiony #ff0000"..result[1]["buyers"].." razy#ffffff\n#ffffffDostępne: "..pcscolor..""..result[1]["pcs"].." szt\n\n#ffffffKoszt pojazdu: #00ff00"..result[1]["price"].." pkt")
end)

function onselected(current,previous)
	if cmtaSALON then
	triggerServerEvent("cmta_salon:infoveh", resourceRoot,dgsComboBoxGetItemText(scrollpanel, current))
	end
end
